<script setup>
import { onMounted, nextTick, ref, reactive, watch, toRefs, watchEffect, onUnmounted } from 'vue'
import mitt from '@/assets/js/mitt'
import zhong from '@/assets/img/zhong.png'
import play from '@/assets/img/play.png'
const props = defineProps({
  allSteps: {
    type: Array,
    default: () => [
      {
        time: '08-01',
      },
      {
        time: '08-02',
      },
      {
        time: '08-03',
      },
      {
        time: '08-04',
      }
    ]
  },
  stepNum: {
    type: Number,
    default: 0
  },
  isWx: {
    type: Boolean,
    default: false
  }
})
const { allSteps, stepNum } = toRefs(props)

onMounted(() => {
  // mitt.on('currentStepChange', (data) => {
  //   currentStep.value = data
  //   stepWidth.value = `${(currentStep.value) / (allSteps.value.length - 1) * 50}vw`
  // })
})
const dayStep = ref([])
const stepWidth = ref('0vw')
const currentStep = ref(0)
const emit = defineEmits(['queryPng'])

const nextStep = () => {
  // 通过改变stepWidth的宽度来改变颜色
  if (currentStep.value < allSteps.value.length - 1) {
    currentStep.value++
    stepWidth.value = `${(currentStep.value) / (allSteps.value.length - 1) * 50}vw`
    // 时间由2021年09月06日01时转为2021090601
    let time = allSteps.value[currentStep.value].time.split('年')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[1].split('日')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[1].split('日')[1].split('时')[0]
    emit('queryPng', time)
  }
}
const lastStep = () => {
  if (currentStep.value > 0) {
    currentStep.value--
    stepWidth.value = `${(currentStep.value) / (allSteps.value.length - 1) * 50}vw`
    let time = allSteps.value[currentStep.value].time.split('年')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[1].split('日')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[1].split('日')[1].split('时')[0]

    emit('queryPng', time)
  }
}

const isPlay = ref(false)
let nextEvent = null
const stopStep = () => {
  isPlay.value = !isPlay.value
  if (isPlay.value) {
    nextEvent = setInterval(() => {
      nextStep()
    }, 2000)
  } else {
    clearInterval(nextEvent)
  }
}

const selectDay = (e) => {
  let timeDiv = document.getElementsByClassName('step_img')[0]
  let step = Math.round(e.offsetX / timeDiv.offsetWidth * (allSteps.value.length - 1))
  currentStep.value = step
  stepWidth.value = `${(currentStep.value) / (allSteps.value.length - 1) * 50}vw`
  let time = allSteps.value[currentStep.value].time.split('年')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[1].split('日')[0] + allSteps.value[currentStep.value].time.split('年')[1].split('月')[1].split('日')[1].split('时')[0]

  emit('queryPng', time)
}

// watchEffect(() => {
//   if (allSteps.value.length == 0) return
//   if (allSteps.value) {
//     currentStep.value = Number(stepNum.value)
//     stepWidth.value = `${(currentStep.value) / (allSteps.value.length - 1) * 50}vw`
//     isPlay.value = false
//     clearInterval(nextEvent)

//     // allSteps.value中新开始的一天加入到dayStep中，格式为{time: '08-01'}
//     dayStep.value = []
//     for (let i = 0; i < allSteps.value.length; i++) {
//       if (i == 0) {
//         dayStep.value.push({ time: allSteps.value[i].time, step: i })
//       } else {
//         // 获取allSteps.value[i].time中的数字
//         let timeDay = allSteps.value[i].time.split('月')[1].split('日')[0]
//         if (timeDay != allSteps.value[i - 1].time.split('月')[1].split('日')[0]) {
//           dayStep.value.push({ time: allSteps.value[i].time, step: i })
//         }
//       }
//     }
//   }
// })

watch(allSteps, (val) => {

  currentStep.value = Number(stepNum.value)
  stepWidth.value = `${(currentStep.value) / (allSteps.value.length - 1) * 50}vw`
  isPlay.value = false
  clearInterval(nextEvent)
  // allSteps.value中新开始的一天加入到dayStep中，格式为{time: '08-01'}
  dayStep.value = []
  for (let i = 0; i < allSteps.value.length; i++) {
    if (i == 0) {
      dayStep.value.push({ time: allSteps.value[i].time, step: i })
    } else {
      // 获取allSteps.value[i].time中的数字
      let timeDay = allSteps.value[i].time.split('月')[1].split('日')[0]
      if (timeDay != allSteps.value[i - 1].time.split('月')[1].split('日')[0]) {
        dayStep.value.push({ time: allSteps.value[i].time, step: i })
      }
    }
  }
})

onUnmounted(() => {
  // mitt.off('currentStepChange')
})
</script>

<template>
  <div class="timeStep">
    <div class="play_c">
      <div class="play_icon" @click="lastStep">
        <img src="/img/zuo.png" alt="">
      </div>
      <div class="play_icon" @click="stopStep">
        <img :src="isPlay ? zhong : play" alt="">
      </div>
      <div class="play_icon" @click="nextStep">
        <img src="/img/you.png" alt="">
      </div>
    </div>
    <div class="step_img" @click="selectDay"></div>
    <div class="step_img_color" :style="{ width: stepWidth }" @click="selectDay"></div>
    <div class="step_img_tip" :style="{ left: stepWidth }">{{ allSteps.length > 0 ? allSteps[currentStep].time : '' }}
    </div>
    <div v-if="!isWx" v-for="(item, index) in dayStep" :key="index" class="step_img_tip_day_item"
      :style="{ left: (item.step) / (allSteps.length - 1) * 50 + 'vw' }">{{ item.time.split('日')[0] }}日
    </div>
  </div>
</template>

<style lang='less' scoped>
.timeStep {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  height: 40px;
  // width: 70%;
  z-index: 1000;
  display: flex;

  .play_c {
    display: flex;
    height: 100%;
    // width: 10%;
    margin-right: 10px;
    cursor: pointer;

    .play_icon {
      height: 40px;
      width: 40px;
      border-radius: 50%;
      background-color: var(--nv-bg-color);
      margin-top: -10px;
      display: flex;
      justify-content: center;
      align-items: center;
      // 不可选中
      user-select: none;

      img {
        height: 20px;
        width: 20px;
      }
    }
  }

  // border: 1px solid tomato;
  .step_img {
    height: 40%;
    // width: 90%;
    width: 50vw;
    border-radius: 14px;
    background-color: var(--nv-bg-color);
    border: 1px solid rgb(39, 208, 216);
    cursor: pointer;
  }

  // 与上面重叠
  .step_img_color {
    position: absolute;
    margin-left: 130px;
    height: 40%;
    // width: 60vw;
    // width: 10%;
    border-radius: 12px;
    background-color: rgb(39, 208, 216);
    transition: width 0.35s;
    cursor: pointer;
  }

  .step_img_tip {
    position: absolute;
    margin-left: 130px;
    height: 40px;
    width: 120px;
    border-radius: 8px;
    background-color: #009CF9;
    transition: width 0.35s;
    color: white;
    font-size: 12px;
    line-height: 40px;
    text-align: center;
    margin-top: -50px;
    transition: left 0.35s;
    transform: translateX(-50%);


    // 三角箭头样式
    &::after {
      content: '';
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: -10px;
      border: 5px solid transparent;
      border-top-color: #009CF9;
    }
  }

  .step_img_tip_day {
    position: absolute;
    margin-left: 130px;
    bottom: 0;
    display: flex;


  }

  .step_img_tip_day_item {
    position: absolute;
    margin-left: 130px;
    // margin-left: 80px;
    bottom: 0;
    // 文字白色阴影
    text-shadow: 0 0 2px #fff;
    color: #009CF9;
    // 文字不换行
    white-space: nowrap;
  }
}
</style>